const cheerio = require('cheerio');
const _ = require('underscore');
const Base = require('./_base')
const path = window.require('path');
const md5 = require('js-md5');

export default class Gather extends Base{
	constructor(reportCb) {
	    super(reportCb);
	    this.enterurl = 'http://www.asnaghi.com';
	    this.enterurli='http://www.asnaghi.com/it/portfolio-3/';
      this.pathname = path.resolve('data/' + md5(this.enterurl))
	  }
	  getProductUrls = async() => {
	  	let urls = []
	  	urls.push(this.enterurli);
	  	this.status.msg = '获取列表'
	  	this.status.current = 1
	  	this.status.count = urls.length
	  	this.reportCb(this.status)
	  	return urls
	  }
	  getProduct = async(url) => {
	  	let res = await fetch(url).then(res => res.text());
	  	let $ = cheerio.load(res)
	  	if($('.grid-item').html()===undefined||$('.grid-item').length<1)return false;
	  	let product={};
	  	product.images=[];
	  	$('.grid-item').each(function(j,el){
	  		product.origin_html=$('html').html();
	  		product.origin_url = url;
	  		product.brand_num='asnaghi';
	  		product.date=new Date().getTime();
	  		product.local_path=[];
	  		let img=$('a img',$(this).html()).attr('src');
	  		// if(!img.startsWith('http:')) img = 'http:'+img
	  		product.images.push({"pic_url": img});
	  		product.name=$('.project-title',$(this).html()).text();
	  	});
	  	return product
	  }
}
